function createXHR()
{
    try 
    {
        return new XMLHttpRequest();
    } catch (e) 
    {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function add_user_form()
{
  var xmlhttp = createXHR();
  xmlhttp.onreadystatechange=function()
  {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
     	document.getElementById("home").innerHTML=xmlhttp.responseText;
     }
  }
   
 xmlhttp.open("POST","add_user_form.php",true);
 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlhttp.send();
}

function validate_add_user_form()
{
 var ID = document.getElementsByName("ID")[0].value;
 var name = document.getElementsByName("name")[0].value;
 var password = document.getElementsByName("password")[0].value;
 var email = document.getElementsByName("email")[0].value;
 var user_type = document.getElementsByName("user_type")[0].value;
 var specialization = document.getElementsByName("specialization")[0].value;
 var level = document.getElementsByName("level")[0].value;
 var subSpecialization = document.getElementsByName("subSpecialization")[0].value;
 var user_type = document.getElementById("user_type").value;
 var sub = document.getElementById("specialization").value;

 if( ID == "")
   {
     document.getElementsByClassName("required")[0].innerHTML = "<font color=red>* Must not be empty</font>";
   }
 if( name == "")
   {
     document.getElementsByClassName("required")[1].innerHTML = "<font color=red>* Must not be empty</font>";
   }
 if( email == "")
   {
     document.getElementsByClassName("required")[2].innerHTML = "<font color=red>* Must not be empty</font>";
   }
  if( password == "")
   {
     document.getElementsByClassName("required")[3].innerHTML = "<font color=red>* Must not be empty</font>";
   }
 if( specialization == "")
   {
     document.getElementsByClassName("required")[4].innerHTML = "<font color=red>* Must not be empty</font>";
   }
 if( user_type == "")
   {
     document.getElementsByClassName("required")[5].innerHTML = "<font color=red>* Must not be empty</font>";
   }

  if ((user_type == "student") && (sub == 1))
    {
      if( level == "")
        {
         document.getElementsByClassName("required")[6].innerHTML = "<font color=red>* Must not be empty</font>";
        }
    }
  if ((user_type == "student") && (sub != 1))
   {
      if( level == "")
        {
         document.getElementsByClassName("required")[6].innerHTML = "<font color=red>* Must not be empty</font>";
        }
      if(subSpecialization == "")
        {
          document.getElementsByClassName("required")[7].innerHTML = "<font color=red>* Must not be empty</font>";
        }
   }

 if( ID != "")
   {
     document.getElementsByClassName("required")[0].innerHTML = "";
   }
 if( name != "")
   {
     document.getElementsByClassName("required")[1].innerHTML = "";
   }
 if( email != "")
   {
     var atpos = email.indexOf("@");
     var dotpos = email.lastIndexOf(".");
     if (atpos < 1 || dotpos < atpos+2 || dotpos+2 >= email.length)
      {
       document.getElementsByClassName("required")[2].innerHTML = "<font color=red>* Invalid e_mail</font>";
      }
      else
      {
        document.getElementsByClassName("required")[2].innerHTML = "";
      }
   }
 if( password != "")
   {
     document.getElementsByClassName("required")[3].innerHTML = "";
   }
 if( specialization != "")
   {
     document.getElementsByClassName("required")[4].innerHTML = "";
   }
 if( user_type != "")
   {
     document.getElementsByClassName("required")[5].innerHTML = "";
   }

  if ((user_type == "student") && (sub == 1))
    {
      if( level != "")
        {
         document.getElementsByClassName("required")[6].innerHTML = "";
        }
    }
  if ((user_type == "student") && (sub != 1))
   {
      if( level != "")
        {
         document.getElementsByClassName("required")[6].innerHTML = "";
        }
      if(subSpecialization != "")
        {
          document.getElementsByClassName("required")[7].innerHTML = "";
        }
   }
   submit();
}

function submit ()
{
  var required = document.getElementsByClassName("last_check");
  var check = true;

 for ( var i =0 ; i < required.length ; i++)
  {
    if (required[i].innerHTML != "") 
    {
     check = false;
    }
  }
  if (check)
  {
    var submit_button =document.getElementById("submit")
    submit_button.type = "submit";
  }

}

function subspecialization(value)
{
  var user_type = document.getElementById("user_type").value;
  var sub = document.getElementById("specialization").value;

  if (  (sub != 1) && (user_type == "student") )
    {
       document.getElementById("sub").style.display="table-row";
       document.getElementById("level").style.display="table-row";

       var select = document.getElementById("subSpecializationSelect");

       for(i = 0 ; i < select.childNodes.length ; i++)
        {
            select.childNodes[i].disabled = false;
        }
       for(i = 0 ; i < select.childNodes.length ; i++)
        {
           if (select.childNodes[i].value == sub)
            {
              select.value = "";
              select.childNodes[i].disabled= true;
              break;      
            }
        }
    } 
    else if (user_type == "student" && (sub == 1) ) 
     {
       document.getElementById("level").style.display="table-row";
       document.getElementById("sub").style.display="none"; 
     }
    else if (user_type != "student")
     {
     document.getElementById("level").style.display="none"; 
     document.getElementById("sub").style.display="none";  
     }
 else 
    {
     document.getElementById("sub").style.display="none";  
     document.getElementById("level").style.display="none";  
    }
}

function validate_add_faculty_form()
{
 var name = document.getElementsByName("name")[0].value;
 var description = document.getElementsByName("description")[0].value;
 var position = document.getElementsByName("position")[0].value;
 var date = document.getElementsByName("date")[0].value;

 if( (name == "") || (name = null)
   {
     document.getElementsByClassName("required")[0].innerHTML = "<font color=red>* Must not be empty</font>";
   }
 if( (description == "") || (description == null))
   {
     document.getElementsByClassName("required")[1].innerHTML = "<font color=red>* Must not be empty</font>";
   }
 if(( position == "") || ( position == null))
   {
     document.getElementsByClassName("required")[2].innerHTML = "<font color=red>* Must not be empty</font>";
   }
 if(( date == "") || ( date == null))
   {
     document.getElementsByClassName("required")[3].innerHTML = "<font color=red>* Must not be empty</font>";
   }

 if(( name != "") || ( name != null))
   {
     document.getElementsByClassName("required")[0].innerHTML = "";
   }
 if(( description != "") || ( description != null))
   {
     document.getElementsByClassName("required")[1].innerHTML = "";
   }
 if(( position != "") || ( position != null))
   {
     document.getElementsByClassName("required")[2].innerHTML = "";
   }
 if(( date != "") || ( date != null))
   {
     document.getElementsByClassName("required")[3].innerHTML = "";
   }
 
   submit();
}

function add_resource_form()
{
  var xmlhttp = createXHR();
  xmlhttp.onreadystatechange=function()
  {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
      document.getElementById("home").innerHTML=xmlhttp.responseText;
     }
  }
   
 xmlhttp.open("POST","add_resource_form.php",true);
 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlhttp.send();
}

function resource(value)
{
  room_fields = document.getElementsByClassName("hiddenR");
  equipment_fields = document.getElementsByClassName("hiddenE");

  if ( value == "R")
    {
       for(i = 0 ; i < room_fields.length ; i++)
        {
         room_fields[i].style.display = "table-row";
      
        }
       for(i = 0 ; i < equipment.length ; i++)
        {
            equipment_fields[i].style.display = "none";
        }
    } 
    else if (value == "E" ) 
     {
       for(i = 0 ; i < room_fields.length ; i++)
        {
         room_fields[i].style.display = "none";
      
        }
       for(i = 0 ; i < equipment.length ; i++)
        {
            equipment_fields[i].style.display = "table-row";
        }
     }
}

function validate_add_resource_form()
{
 var resource_type = document.getElementsByName("resource_type")[0].value;
 var room_type = document.getElementsByName("room_type")[0].value;
 var equipment_type = document.getElementsByName("name")[0].value;
 var name = document.getElementsByName("name")[0].value;
 var description = document.getElementsByName("description")[0].value;
 var capacity = document.getElementsByName("capacity")[0].value;
 var position = document.getElementsByName("position")[0].value;
 var date = document.getElementsByName("date")[0].value;
 var model = document.getElementsByName("model")[0].value;
 var state = document.getElementsByName("state")[0].value;
 var check = true;

 if( (resource_type == "") || (resource_type == null))
   {
     document.getElementsByClassName("required")[0].innerHTML = "<font color=red>* Must not be empty</font>";
   }
 if(( name == "" ) || ( name == null))
   {
     document.getElementsByClassName("required")[1].innerHTML = "<font color=red>* Must not be empty</font>";
   }
 if(( description == "") || ( name == null ))
   {
     document.getElementsByClassName("required")[2].innerHTML = "<font color=red>* Must not be empty</font>";
   }
 if( resource_type == "R")
   {
     if(( room_type == "") || ( room_type == null))
     {
       document.getElementsByClassName("requiredR")[0].innerHTML = "<font color=red>* Must not be empty</font>";
      }
     if( capacity == "")
      {
        document.getElementsByClassName("requiredR")[1].innerHTML = "<font color=red>* Must not be empty</font>";
      }
     if( position == "")
     {
       document.getElementsByClassName("requiredR")[2].innerHTML = "<font color=red>* Must not be empty</font>";
      }
   }

 if( resource_type == "E")
   {
     if(( equipment_type == "") || ( equipment_type == null))
      {
        document.getElementsByClassName("requiredE")[0].innerHTML = "<font color=red>* Must not be empty</font>";
      }
    if(( date == "") || ( date == null))
     {
       document.getElementsByClassName("requiredE")[1].innerHTML = "<font color=red>* Must not be empty</font>";
      }
     if(( model == "") || ( model == null))
      {
        document.getElementsByClassName("requiredE")[2].innerHTML = "<font color=red>* Must not be empty</font>";
      }
    if(( state == "") || ( state == null))
     {
       document.getElementsByClassName("requiredE")[3].innerHTML = "<font color=red>* Must not be empty</font>";
      }
    }

 if(( resource_type != "") || ( resource_type != null))
   {
     document.getElementsByClassName("required")[0].innerHTML = "";
   }
 if(( name != "") || ( name != null))
   {
     document.getElementsByClassName("required")[1].innerHTML = "";
   }
 if(( description != "") || ( description != null))
   {
     document.getElementsByClassName("required")[2].innerHTML = "";
   }
 if( resource_type == "R")
   {
     if( room_type != "")
     {
       document.getElementsByClassName("requiredR")[0].innerHTML = "";
      }
     if( capacity != "")
      {
        document.getElementsByClassName("requiredR")[1].innerHTML = "";
      }
     if( position != "")
     {
       document.getElementsByClassName("requiredR")[2].innerHTML = "";
      }
   }

 if( resource_type == "E")
   {
     if( equipment_type != "")
      {
        document.getElementsByClassName("requiredE")[0].innerHTML = "";
      }
    if( date != "")
     {
       document.getElementsByClassName("requiredE")[1].innerHTML = "";
      }
     if( model != "")
      {
        document.getElementsByClassName("requiredE")[2].innerHTML = "";
      }
    if( state != "")
     {
       document.getElementsByClassName("requiredE")[3].innerHTML = "";
      }
   }
   submit();
}
/*
function manage_form(str)
{
  var xmlhttp = createXHR();
  xmlhttp.onreadystatechange=function()
  {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
      
     }
  }
   
 xmlhttp.open("POST",str+".php",true);
 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlhttp.send();
}
*/
function request_free_time_form(str)
{
  var xmlhttp = createXHR();
  xmlhttp.onreadystatechange=function()
  {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
      document.getElementById("home").innerHTML=xmlhttp.responseText;
      choose_radio_button();
     }
  }
   
 xmlhttp.open("POST","request_free_time_form.php",true);
 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlhttp.send();
 }

function choose_radio_button ()
{
  var table_radio = document.getElementById("table_radio");
     table_radio.addEventListener("change",function(e)
     {
         if(e.target && e.target.className == "choose") 
         {
           staff = document.getElementsByClassName("checkbox");
            for (i=0 ; i < staff.length ; i++)
            {
               staff[i].checked = false;
            }

           if(e.target.value == "all" )
           {
            for (i=0 ; i < staff.length ; i++)
            {
               staff[i].checked = true;
            }
           }   
           else if(e.target.value == "doctors" )
           {
            for (i = 0 ; i < staff.length ; i++)
            {
              if(staff[i].parentNode.parentNode.getElementsByTagName("td")[2].innerHTML== "doctor")
              {
                staff[i].checked = true;
              }
            }
           }   
           else if(e.target.value == "teachers" )
           {
            for (i=0 ; i < staff.length ; i++)
            {
              if(staff[i].parentNode.parentNode.getElementsByTagName("td")[2].innerHTML == "teacher")
             {
               staff[i].checked = true;
             }
            }
           }   
         }
         else
         {
          
         }
     });
}

function validate_request_free_time_form()
{
  var staff = document.getElementsByClassName("checkbox");
  var length = 0;
  for (i=0 ; i < staff.length ; i++)
   {
     if(staff[i].checked == true)
     {
      length++;
     }
   }
 if(length != 0)
 {
    var submit_button =document.getElementById("submit")
    submit_button.type = "submit";
 }
}




